


class WalkCommand extends command.Command {

    private from: number;
    private to: number;

    constructor(from: number, to: number) {
        super();
        this.from = from;
        this.to = to;
    }

    execute(notifyFinish: Function) {

        console.log(`开始走路！！！从${this.from}出发`);
        const map = mapManager.getMap();
        map.grid.setEndNode(this.from, this.to);
        const findpath = new astar.AStar();
        findpath.setHeurisitic(findpath.diagonal)
        const result = findpath.findPath(map.grid);

        const path = findpath._path;

        if (path) {
            const intervalCount = setInterval(() => {
                const currentNode = path.shift();
                if (currentNode) {
                    user.goto(currentNode.x, currentNode.y);
                    map.grid.setStartNode(currentNode.x, currentNode.y);
                }
                else {
                    notifyFinish();
                    if (user.view.x == map.transmissionX*TILE_SIZE && user.view.y == map.transmissionY*TILE_SIZE) {
                        user.dispatchEvent("changeMap", { evendata:111 });
                        map.musicAudio.pause();
                        map.musicAudio.currentTime=0;
                        
                    }
                    
                    clearInterval(intervalCount);
                    
                }
            }, 200);
        }
        else {

        }
    }
}
class BattleCommand extends command.Command{
    private monsters: monster;
    private audioBattle:HTMLAudioElement;
    constructor(monsters: monster) {
        super();
        this.monsters = monsters;
    }
    execute(notifyFinish: Function) {
        if(openNode=="open"){
            return ;
        }else{
       const dialog = new monsterDialog(this.monsters);
       this.audioBattle=document.createElement("audio");
       this.audioBattle.src="战斗.mp3";
       this.audioBattle.play();
       monsterDialogContainer.addChild(dialog);
       dialog.addEventListener("closebattle",() =>{
           monsterDialogContainer.removeChild(dialog);
            this.audioBattle.pause();
            this.audioBattle.currentTime=0;
       });
       openNode="open";
    

    }
    
}
           
}

class PickCommand extends command.Command {


    private weapon: Item;
    constructor(weapon: Item) {
        super();
        this.weapon = weapon;
    }

    execute(notifyFinish: Function) {
        user.pick(this.weapon)
        // user.level++;
        const map = mapManager.getMap();
        console.log(map.itemConfig);
        console.log(9999);
        map.removeItem(this.weapon);
        notifyFinish();
    }
}

class GotoAnotherMap extends command.Command{
    constructor(private mapID: string) {
        super();
    }
    execute() {
        
        mapManager.changeMap(this.mapID,user.view.x/TILE_SIZE,user.view.y/TILE_SIZE);
   } 
}

class TalkCommand extends command.Command {

    private npc: Npc;
    constructor(npc: Npc) {
        super();
        this.npc = npc;
    }


    execute(notifyFinish: Function) {
        console.log(`开始对话:${this.npc.toString()}`)
        let mission: Mission | null = null;
        if (this.npc.canSubmitMissions.length > 0) {
            mission = this.npc.canSubmitMissions[0];
            
        }
        if (this.npc.canAcceptMissions.length > 0) {
            mission = this.npc.canAcceptMissions[0];
        }
        else {
       
        }
        console.log(1111);
        console.log(mission);
        console.log(1111);
        
        if (mission) {
            console.log(666666);
            const dialog = new TalkDialog(this.npc);
            // let num=0;
            dialogContainer.addChild(dialog);
            dialog.setMission(mission);
            dialog.addEventListener('close', () => {
                dialogContainer.removeChild(dialog)
                if (mission) {
                    if (mission.status == MissionStatus.CAN_SUBMIT) {
                        console.log('提交任务', mission.title)
                        missionManager.submitMission(mission);
   
                    }
                    else if (mission.status == MissionStatus.CAN_ACCEPT) {
                        console.log('接受任务', mission.title)
                        missionManager.acceptMission(mission);
                    }
                }
                notifyFinish();

            })
        }
        else {
            notifyFinish();
        }






    }
}
